package com.treeeroot.rabbitmq;

import com.rabbitmq.client.*;

import java.io.IOException;

public class Consumer {
    private static final String QUEUE_NAME = "quicker-demo";

    public static void main(String[] args) {
        try {
            //1.创建链接
            Connection connection = RabbitMQConnection.getConnection();
            //2.创建通道
            Channel channel = connection.createChannel();
            DefaultConsumer defaultConsumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    String msg = new String(body, "UTF-8");
                    System.out.println("消费者获取消息:" + msg);
                }
            };
            //3.监听队列
            // autoAck: true 自动签收==while true
            channel.basicConsume(QUEUE_NAME, true, defaultConsumer);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

